---
import ShikiCodeBlock from './ShikiCodeBlock.astro'
import { CODE_EXAMPLES } from './code-examples'
---

<!-- Code Example Section -->
<section class="code-section" role="region" aria-labelledby="code-heading">
  <div class="container">
    <h2 id="code-heading" class="section-title">
      <span class="title-main">Code Examples</span>
      <span class="title-sub">
        Explore different Reatom features • Hover over highlighted elements to
        learn more
      </span>
    </h2>

    <div class="tabs-container">
      <!-- Tab Content (must come first for CSS selectors to work) -->
      <div class="tab-content">
        {
          CODE_EXAMPLES.map((example, index) => (
            <div id={example.id} class="tab-panel" role="tabpanel">
              <ShikiCodeBlock
                code={example.code}
                annotations={example.annotations}
                filename={`${example.id}.tsx`}
                playgroundUrl="https://stackblitz.com/github/reatom/reatom/tree/v1000/examples/react-search"
              />
            </div>
          ))
        }
      </div>

      <!-- Tab Navigation -->
      <!-- Ordered after `.tab-content` for `~` selector -->
      <nav class="tab-nav" role="tablist">
        {
          CODE_EXAMPLES.map((example, index) => (
            <a
              href={`#${example.id}`}
              class="tab-btn"
              role="tab"
              aria-controls={example.id}
              data-tab={example.id}
            >
              <span class="tab-title">{example.title}</span>
              <span class="tab-desc">{example.description}</span>
            </a>
          ))
        }
      </nav>
    </div>
  </div>

  <!-- Tooltip for annotations -->
  <div class="code-tooltip" id="code-tooltip" role="tooltip"></div>
</section>

<style>
  /* Main Section */
  .code-section {
    padding: 4rem 2rem;
    background: var(--bg-primary);
    position: relative;
    transition: background 0.3s ease;
  }

  .container {
    max-width: 1000px;
    margin: 0 auto;
  }

  /* Section Title */
  .section-title {
    text-align: center;
    margin-bottom: 3rem;
  }

  .title-main {
    display: block;
    font-size: clamp(2rem, 5vw, 3rem);
    font-weight: 700;
    background: linear-gradient(
      135deg,
      var(--signal-color) 0%,
      var(--accent-primary) 50%,
      var(--signal-color) 100%
    );
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 0.5rem;
    transition: background 0.3s ease;
  }

  .title-sub {
    display: block;
    font-size: clamp(0.9rem, 2vw, 1.2rem);
    color: var(--text-muted);
    font-weight: 400;
    transition: color 0.3s ease;
  }

  .tabs-container {
    display: flex;
    flex-direction: column-reverse;
  }

  /* Tab Content */
  .tab-content {
    min-height: 500px;
    position: relative;
    margin-bottom: 2rem;
    transition: all 0.3s ease;
  }

  .tab-panel {
    display: none;
  }
  .tab-panel:target {
    display: block;
  }
  .tab-content:not(:has(:target)) .tab-panel:first-child {
    display: block;
  }

  /* Tab Navigation */
  .tab-nav {
    display: flex;
    gap: 1rem;
    justify-content: space-between;
    margin-bottom: 1rem;
  }

  .tab-btn {
    display: inline-block;
    background: var(--card-bg);
    border: 2px solid var(--card-border);
    padding: 1rem 1.5rem;
    border-radius: 0.5rem;
    cursor: pointer;
    transition: all 0.2s ease;
    color: var(--text-primary);
    text-align: start;
    text-decoration: none;
    backdrop-filter: blur(10px);
    flex: 1;
  }

  .tab-btn:hover {
    border-color: var(--accent-primary);
    background: var(--bg-secondary);
  }

  .tab-btn:focus {
    outline: 2px solid var(--accent-primary);
    outline-offset: 2px;
  }

  .tab-title {
    display: block;
    font-size: 1.1rem;
    font-weight: 600;
    margin-bottom: 0.25rem;
    transition: color 0.3s ease;
  }

  .tab-desc {
    display: block;
    font-size: 0.85rem;
    color: var(--text-muted);
    line-height: 1.3;
    transition: color 0.3s ease;
  }

  /* Highlight the active tab link */
  .tab-content:has(#counter:target) ~ .tab-nav .tab-btn[href='#counter'],
  .tab-content:has(#forms:target) ~ .tab-nav .tab-btn[href='#forms'],
  .tab-content:has(#data-loading:target) ~ .tab-nav .tab-btn[href='#data-loading'],
  .tab-content:has(#routing:target) ~ .tab-nav .tab-btn[href='#routing'] {
    border-color: var(--accent-primary);

    .tab-title {
      color: var(--accent-primary);
    }
    .tab-desc {
      color: var(--text-secondary);
    }
  }

  /* Highlight the first tab when no target */
  .tab-content:not(:has(:target)) ~ .tab-nav .tab-btn[href='#core'] {
    .tab-title {
      color: var(--accent-primary);
    }
    .tab-desc {
      color: var(--text-secondary);
    }
  }

  /* Tooltip */
  .code-tooltip {
    position: absolute;
    background: var(--overlay-bg);
    color: var(--text-primary);
    padding: 0.75rem 1rem;
    border-radius: 8px;
    font-size: 0.85rem;
    max-width: 300px;
    z-index: 9999;
    opacity: 0;
    visibility: hidden;
    transition:
      opacity 0.2s ease,
      visibility 0.2s ease;
    border: 1px solid var(--border-color);
    backdrop-filter: blur(10px);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
    pointer-events: none;
    white-space: pre-wrap;
    word-wrap: break-word;
    line-height: 1.4;
    /* Initial position off-screen to prevent flicker */
    top: -9999px;
    left: -9999px;
  }

  .code-tooltip.visible {
    opacity: 1;
    visibility: visible;
  }

  /* Arrow styles for floating-ui arrow middleware (if added later) */
  .code-tooltip[data-popper-placement^='top'] > .tooltip-arrow {
    bottom: -4px;
  }

  .code-tooltip[data-popper-placement^='bottom'] > .tooltip-arrow {
    top: -4px;
  }

  .code-tooltip[data-popper-placement^='left'] > .tooltip-arrow {
    right: -4px;
  }

  .code-tooltip[data-popper-placement^='right'] > .tooltip-arrow {
    left: -4px;
  }

  /* Light theme specific adjustments */
  html[data-theme='light'] .title-main {
    background: linear-gradient(
      135deg,
      #1e3a8a 0%,
      var(--accent-primary) 50%,
      #1e3a8a 100%
    );
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
  }

  html[data-theme='light'] .code-tooltip {
    box-shadow: 0 8px 32px rgba(21, 17, 52, 0.15);
  }

  /* Responsive */
  @media (max-width: 768px) {
    .code-section {
      padding: 3rem 1rem;
    }

    .tab-nav {
      flex-direction: column;
      align-items: center;
    }

    .tab-btn {
      min-width: 280px;
      max-width: 100%;
    }

    .section-title {
      margin-bottom: 2rem;
    }

    .code-tooltip {
      max-width: 250px;
      font-size: 0.8rem;
    }
  }

  @media (max-width: 480px) {
    .tab-btn {
      min-width: 250px;
      padding: 0.8rem 1rem;
    }

    .tab-title {
      font-size: 1rem;
    }

    .tab-desc {
      font-size: 0.8rem;
    }
  }
</style>
